//
//  HMStatusOriginalView.swift
//  WeiBo
//
//  Created by Apple on 16/9/21.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit
import SDWebImage

//  原创微博视图
class HMStatusOriginalView: UIView {
    //  原创微博底部配图
    var originalViewBottomConstraint: Constraint?
    
    var statusViewModel: HMStatusViewModel? {
        didSet {
            //  判断头像
            if let imageUrl = statusViewModel?.status?.user?.profile_image_url {
                userImageView.sd_setImage(with: URL(string: imageUrl)!, placeholderImage: UIImage(named: "avatar_default_big"))
                
            }
            
            //  昵称
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
            //  时间
            timeLabel.text = statusViewModel?.status?.created_at
            //  来源
            sourceLabel.text = "来自星星的你"
            //  微博内容
            contentLabel.text = statusViewModel?.status?.text
            //  先卸载之前的约束
            originalViewBottomConstraint?.uninstall()
            //  有配图
            if let picUrls = statusViewModel?.status?.pic_urls, picUrls.count > 0 {

                //  绑定数据
                pictureView.picUrls = picUrls
                pictureView.isHidden = false
                self.snp_updateConstraints(closure: { (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HMHomeTableViewCellMargin).constraint
                })
               
                
                
            } else {
                self.snp_updateConstraints(closure: { (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(HMHomeTableViewCellMargin).constraint
                })
                pictureView.isHidden = true
            }
            
            
        }
    }

    //  用户头像
    private lazy var userImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //  认证类型图片
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //  昵称
    private lazy var screenNameLabel: UILabel = UILabel(textColor: UIColor.darkGray, fontSize: 13)
    //  会员等级图片
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //  时间
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orange, fontSize: 11)
    //  来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGray, fontSize: 11)
    //  微博内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGray, fontSize: 13)
        label.numberOfLines = 0
        return label
    }()
    
    //  配图
    private lazy var pictureView: HMStatusPictureView = {
        let view = HMStatusPictureView()
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //  添加控件设置约束
    private func setupUI() {
        backgroundColor = UIColor.white
        
        addSubview(userImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //
        userImageView.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(HMHomeTableViewCellMargin)
            make.leading.equalTo(self).offset(HMHomeTableViewCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
            
        }
        verifiedTypeImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(userImageView.snp_trailing)
            make.centerY.equalTo(userImageView.snp_bottom)
        }
        screenNameLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(userImageView.snp_trailing).offset(HMHomeTableViewCellMargin)
            make.top.equalTo(userImageView)
        }
        mbrankImageView.snp_makeConstraints { (make) in
            make.top.equalTo(screenNameLabel)
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(HMHomeTableViewCellMargin)
        }
        timeLabel.snp_makeConstraints { (make) in
            make.bottom.equalTo(userImageView)
            make.leading.equalTo(userImageView.snp_trailing).offset(HMHomeTableViewCellMargin)
        }
        sourceLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(timeLabel.snp_trailing).offset(HMHomeTableViewCellMargin)
            make.top.equalTo(timeLabel)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(userImageView)
            make.top.equalTo(userImageView.snp_bottom).offset(HMHomeTableViewCellMargin)
            make.width.equalTo(ScreenWidth - 2 * HMHomeTableViewCellMargin)
            
        }
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(HMHomeTableViewCellMargin)
            make.leading.equalTo(contentLabel)
            
        }
        //  关键约束，让原创微博的底部等于内容的底部加上一个间距
        self.snp_makeConstraints { (make) in
            originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HMHomeTableViewCellMargin).constraint
        }
        
    }
    
    

}
